home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-03 / chain11.zip / CHAIN11.BAS < prev    next >
BASIC Source File  |  1992-08-26  |  10KB  |  255 lines

  1. DEFDBL A-Z
  2. CONST FALSE = (0 = 1)
  3. CONST TRUE = (1 = 1)
  4. CONST PI = 3.141592653589#
  5. CONST Rad2Deg = 180 / PI
  6. DEF fnMargin (x) = x * 1.000001
  7. CONST Video = TRUE
  8.  
  9. Init:
  10.   PRINT "CHAIN generator for POVRay 1.0  by K. Koehler  v1.1"
  11.   PRINT
  12.   ' 08-10-92 KJK New program (@ Inlaws between houses)
  13.   ' 08-26-92 KJK Minor tweeks: Build chain top-down.
  14.          Now builds circular chain.
  15.  
  16.   INPUT "Link Inside Diameter     :", LinkID
  17.   INPUT "Metal Radius             :", MetalRadius
  18.   Structure$ = ""
  19.   WHILE Structure$ = ""
  20.     INPUT "Structure (Straight/Arc) :", Structure$
  21.     Structure$ = LEFT$(UCASE$(Structure$), 1)
  22.     IF Structure$ <> "S" AND Structure$ <> "A" THEN Structure$ = ""
  23.   WEND
  24.   IF Structure$ = "S" THEN INPUT "Number of Links          :", Links
  25.   IF Structure$ = "A" THEN
  26.     ArcMethod$ = ""
  27.     WHILE ArcMethod$ = ""
  28.       INPUT "Arc Method (Links/Radius):", ArcMethod$
  29.       ArcMethod$ = LEFT$(UCASE$(ArcMethod$), 1)
  30.       IF ArcMethod$ <> "L" AND ArcMethod$ <> "R" THEN ArcMethod$ = ""
  31.     WEND
  32.     IF ArcMethod$ = "R" THEN INPUT "Arc Radius               :", ArcRadius
  33.     IF ArcMethod$ = "L" THEN INPUT "Number of Links/360 deg. :", Links
  34.   END IF
  35.   LinkUnit = 1
  36.   LinkHieght = LinkUnit / 2 + MetalRadius
  37.   LinkWidth = LinkID + MetalRadius
  38.   LoopOrigin = LinkHieght - LinkWidth
  39.   a9 = -2 * (LinkWidth ^ 2 + MetalRadius ^ 2)    ' torus stuff
  40.   a25 = 2 * (LinkWidth ^ 2 - MetalRadius ^ 2)    ' torus stuff
  41.   a34 = (LinkWidth ^ 2 - MetalRadius ^ 2) ^ 2    ' torus stuff
  42.   if Structure$="S" then
  43.     Distance= -1.2 * LinkUnit * Links
  44.   endif
  45.   IF Structure$ = "A" THEN
  46.     IF ArcMethod$ = "R" THEN
  47.       ArcIncrement = ATN((LinkUnit / 2) / SQR(((LinkUnit / 2) ^ 2) + (ArcRadius ^ 2))) * Rad2Deg * 2
  48.       Links=360/ArcIncrement
  49.     END IF
  50.     IF ArcMethod$ = "L" THEN
  51.       ArcIncrement = 360 / Links
  52.       ArcRadius = (LinkUnit / 2) / TAN((ArcIncrement / 2) / Rad2Deg)
  53.     END IF
  54.     ArcBoundIn = ArcRadius
  55.     ArcBoundOut = SQR(((LinkUnit / 2) ^ 2) + (ArcRadius ^ 2))
  56.     Distance= -2.8 * ArcBoundOut
  57.   END IF
  58.   MaxX = LinkWidth + MetalRadius
  59.   MaxY = LinkHieght + MetalRadius
  60.   MaxZ = MetalRadius
  61.   MaxXYZ = MaxX
  62.   IF MaxY > MaxXYZ THEN MaxXYZ = MaxY
  63.   IF MaxZ > MaxXYZ THEN MaxXYZ = MaxZ
  64.   IF Video THEN SCREEN 1: COLOR 7
  65.   wy = MaxXYZ * 1.1              ' allow % extra screen space
  66.   wx = wy * 1.5               ' adjust aspect ratio
  67.   IF Video THEN WINDOW (-wx, -wy)-(wx, wy)
  68.  
  69. BuildExample:
  70.   OPEN "chain.pov" FOR OUTPUT AS #1
  71.   GOSUB DatTop
  72.   GOSUB DatBottom
  73.   CLOSE #1
  74.  
  75. BuildObject:
  76.   OPEN "chain.inc" FOR OUTPUT AS #1
  77.   PRINT #1, ""
  78.   PRINT #1, "#declare ChainTex0  = texture { Shiny  colour Red  }"
  79.   PRINT #1, "#declare ChainTex1  = texture { Shiny  colour Blue }"
  80.   PRINT #1, ""
  81.   PRINT #1, "#declare Torus ="
  82.   PRINT #1, "   quartic {"
  83.   PRINT #1, "      <1.0 0.0 0.0 0.0 2.0 0.0 0.0 2.0 0.0 "
  84.   PRINT #1, "      "; a9; " 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0"
  85.   PRINT #1, "       0.0 0.0 1.0 0.0 0.0 2.0 0.0 "; a25; " 0.0"
  86.   PRINT #1, "       0.0 0.0 0.0 1.0 0.0 "; a9; "0.0"; a34; ">"
  87.   PRINT #1, "    } // end_quartic"
  88.   PRINT #1, ""
  89.   PRINT #1, "#declare Link ="
  90.   PRINT #1, "  object {"
  91.   PRINT #1, "  union {"
  92.   PRINT #1, "       // LinkID ="; LinkID; " MetalRadius ="; MetalRadius; " Links ="; Links
  93.   PRINT #1, "    intersection {"
  94.   PRINT #1, USING "      box { <###.#### ###.#### ###.####> "; -(LinkWidth + MetalRadius); -LoopOrigin; -MetalRadius;
  95.   PRINT #1, USING "<###.#### ###.#### ###.####> }"; -(LinkWidth - MetalRadius); LoopOrigin; MetalRadius
  96.   PRINT #1, "      quadric { Cylinder_Y ";
  97.   PRINT #1, USING "scale <###.#### 1 ###.####>  "; MetalRadius; MetalRadius;
  98.   PRINT #1, USING "translate <###.#### 0 0> }"; -LinkWidth
  99.   PRINT #1, "    }"
  100.   IF Video THEN
  101.     LINE (-(LinkWidth + MetalRadius), -LoopOrigin)-(-(LinkWidth + MetalRadius), LoopOrigin), 4
  102.     LINE (-(LinkWidth - MetalRadius), -LoopOrigin)-(-(LinkWidth - MetalRadius), LoopOrigin), 1
  103.   END IF
  104.   PRINT #1, "    intersection {"
  105.   PRINT #1, USING "      box { <###.#### ###.#### ###.####> "; (LinkWidth - MetalRadius); -LoopOrigin; -MetalRadius;
  106.   PRINT #1, USING "<###.#### ###.#### ###.####> }"; (LinkWidth + MetalRadius); LoopOrigin; MetalRadius
  107.   PRINT #1, "      quadric { Cylinder_Y ";
  108.   PRINT #1, USING "scale <###.#### 1 ###.####>  "; MetalRadius; MetalRadius;
  109.   PRINT #1, USING "translate <###.#### 0 0> }"; LinkWidth
  110.   PRINT #1, "    }"
  111.   IF Video THEN
  112.     LINE ((LinkWidth - MetalRadius), -LoopOrigin)-((LinkWidth - MetalRadius), LoopOrigin), 1
  113.     LINE ((LinkWidth + MetalRadius), -LoopOrigin)-((LinkWidth + MetalRadius), LoopOrigin), 4
  114.   END IF
  115.   PRINT #1, "    intersection {"
  116.   PRINT #1, "      plane { <0 -1 0> 0 }"
  117.   PRINT #1, "      quartic { Torus rotate <90 0 0> }"
  118.   PRINT #1, USING "      translate <0 ###.#### 0>"; LoopOrigin
  119.   PRINT #1, "    }"
  120.   IF Video THEN
  121.     CIRCLE (0, LoopOrigin), LinkWidth + MetalRadius, 4, 0, PI
  122.     CIRCLE (0, LoopOrigin), LinkWidth - MetalRadius, 1, 0, PI
  123.   END IF
  124.   PRINT #1, "    intersection {"
  125.   PRINT #1, "      plane { <0  1 0> 0 }"
  126.   PRINT #1, "      quartic { Torus rotate <90 0 0> }"
  127.   PRINT #1, USING "      translate <0 ###.#### 0>"; -LoopOrigin
  128.   PRINT #1, "    }"
  129.   IF Video THEN
  130.     CIRCLE (0, -LoopOrigin), LinkWidth + MetalRadius, 4, PI, 2 * PI
  131.     CIRCLE (0, -LoopOrigin), LinkWidth - MetalRadius, 1, PI, 2 * PI
  132.   END IF
  133.   PRINT #1, "  } // end_union"
  134.   PRINT #1, "  texture { ChainTex0 }"
  135.   PRINT #1, "  bounded_by {"
  136.   PRINT #1, USING "    box { <###.#### ###.#### ###.####> "; -MaxX; -MaxY; -MaxZ;
  137.   PRINT #1, USING "<###.#### ###.#### ###.####> } "; MaxX; MaxY; MaxZ
  138.   PRINT #1, "    } // end_bound"
  139.   PRINT #1, "  } // end_object"
  140.   IF Links > 1 THEN
  141.     IF Structure$ = "S" THEN GOSUB BuildChainStraight
  142.   '  IF Structure$ = "A" AND ArcMethod$ = "R" THEN GOSUB BuildChainLoopRadial
  143.     IF Structure$ = "A" AND ArcMethod$ = "R" THEN GOSUB BuildChainLoopLink
  144.     IF Structure$ = "A" AND ArcMethod$ = "L" THEN GOSUB BuildChainLoopLink
  145.   END IF
  146.   CLOSE #1
  147.   IF Video THEN WHILE INKEY$ = "": WEND
  148. END
  149.  
  150. BuildChainStraight:
  151.   toggle% = FALSE
  152.   Range = (LinkUnit * (Links - 1) / 2)
  153.   PRINT #1,
  154.   PRINT #1, "#declare Chain ="
  155.   PRINT #1, "  composite {"
  156.   PRINT #1, "       // Center of link at chain ends = <0 +/-"; Range; " 0>"
  157.   FOR k = Range TO -Range STEP LinkUnit
  158.     PRINT #1, USING "    object { Link  translate <0 #####.#### 0>  "; k;
  159.     IF toggle% THEN
  160.       PRINT #1, "rotate <0 90 0>  ";
  161.     ELSE
  162.       PRINT #1, "texture { ChainTex1 } ";
  163.     END IF
  164.     toggle% = NOT toggle%
  165.     PRINT #1, "}"
  166.   NEXT k
  167.   PRINT #1, "    bounded_by {"
  168.   PRINT #1, USING "      box { <###.#### ###.#### ###.####> "; -MaxX; -MaxY - Range; -MaxX;
  169.   PRINT #1, USING "<###.#### ###.#### ###.####> } "; MaxX; MaxY + Range; MaxX
  170.   PRINT #1, "    } // end_bound"
  171.   PRINT #1, "  } // end_composite"
  172.   RETURN
  173.  
  174. BuildChainLoopLink:
  175.   toggle% = FALSE
  176.   PRINT #1,
  177.   PRINT #1, "#declare Chain ="
  178.   PRINT #1, "  composite {"
  179.   print #1,     "       // ArcRadius=";ArcRadius;"  Links=";Links
  180.   FOR k = 0 TO Links-1
  181.     PRINT #1, "    object { Link ";
  182.     IF toggle% THEN PRINT #1, "rotate <0 90 0> ";
  183.     PRINT #1, "translate <"; ArcRadius; "0 0> ";
  184.     PRINT #1, "rotate <0 0 "; ArcIncrement * k; "> ";
  185.     IF NOT toggle% THEN PRINT #1, "texture { ChainTex1 } ";
  186.     toggle% = NOT toggle%
  187.     PRINT #1, "}"
  188.   NEXT k
  189.   PRINT #1, "    bounded_by {"
  190.   PRINT #1, "      intersection {"
  191.   PRINT #1, "        quadric { Cylinder_Z  scale <"; ArcBoundOut+MaxY; ArcBoundOut+MaxY; "1> }"
  192.   PRINT #1, "        quadric { Cylinder_Z  scale <"; ArcBoundIn-MaxY; ArcBoundIn-MaxY; "1> inverse }"
  193.   PRINT #1, "        box { <"; -(ArcBoundIn + MaxY); -(ArcBoundIn + MaxY); -MaxY; "> ";
  194.   PRINT #1, "<"; (ArcBoundIn + MaxY); (ArcBoundIn + MaxY); MaxY; "> }"
  195.   PRINT #1, "      }"
  196.   PRINT #1, "    }"
  197.   PRINT #1, "  }"
  198.   RETURN
  199.  
  200. DspFile:
  201.   PRINT #1, ""
  202.   PRINT #1, "camera {"
  203.   PRINT #1, "   location  <0.0 0.0 "; Distance; ">"
  204.   PRINT #1, "   direction <0.0 0.0 1.0>"
  205.   PRINT #1, "   up  <0.0  1.0  0.0>"
  206.   PRINT #1, "   right <1.33333 0.0 0.0>"
  207.   PRINT #1, "   look_at <0.0 0.0 0.0>"
  208.   PRINT #1, "}"
  209.   PRINT #1, ""
  210.   PRINT #1, "/* put down the beloved famous raytrace green/yellow checkered floor */"
  211.   PRINT #1, "object {"
  212.   PRINT #1, "   plane { <0.0 1.0 0.0>"; -(LinkUnit * (Links + 1) / 2); "}"
  213.   PRINT #1, "   texture {"
  214.   PRINT #1, "      checker colour Yellow colour Green"
  215.   PRINT #1, "      scale <"; MaxXYZ / 3 * Links; " "; MaxXYZ / 3 * Links; " "; MaxXYZ / 3 * Links; ">"
  216.   PRINT #1, "      ambient 0.3"
  217.   PRINT #1, "      diffuse 0.7"
  218.   PRINT #1, "   }"
  219.   PRINT #1, "}"
  220.   PRINT #1, ""
  221.   IF Links = 1 THEN
  222.     PRINT #1, "object {"
  223.     PRINT #1, "    Link"
  224.     PRINT #1, "}"
  225.   ELSE
  226.     PRINT #1, "composite {"
  227.     PRINT #1, "    Chain
  228.     print #1, "//  rotate <0 30 0>"
  229.     PRINT #1, "}"
  230.   END IF
  231.   PRINT #1, ""
  232.   PRINT #1, "object { light_source { ";
  233.   PRINT #1, "<"; 10 * MaxXYZ; " "; 12 * MaxXYZ; " "; -13 * MaxXYZ; ">";
  234.   PRINT #1, " colour White } }"
  235. RETURN
  236.  
  237. DatTop:
  238.   PRINT #1, "// Persistence of Vision Raytracer"
  239.   PRINT #1, "#include "; CHR$(34); "shapes.inc"; CHR$(34)
  240.   PRINT #1, "#include "; CHR$(34); "colors.inc"; CHR$(34)
  241.   PRINT #1, "#include "; CHR$(34); "textures.inc"; CHR$(34)
  242.   PRINT #1, ""
  243.   PRINT #1, "//----------------------- Definitions start here -----------------------"
  244. RETURN
  245.  
  246. DatBottom:
  247.   PRINT #1, ""
  248.   PRINT #1, "#include "; CHR$(34); "chain.inc"; CHR$(34)
  249.   PRINT #1, ""
  250.   PRINT #1, "//----------------------- Display starts here --------------------------"
  251.   PRINT #1, ""
  252.   GOSUB DspFile
  253. RETURN
  254.  
  255.